﻿using System.Windows;
using GpgApi;
using WgpgCore;

namespace WgpgGUI
{
    public static class GenerateKey
    {
        public static void Generate(Window owner)
        {
            DialogGenerateKey window = new DialogGenerateKey(owner);
            if (window.ShowDialog() != true)
                return;

            GpgGenerateKey generatekey = new GpgGenerateKey(window.Fullname, window.Email, window.Comment, window.KeyAlgorithm, window.KeySize, window.ExpirationDate);

            TaskManager.Instance.Add(generatekey, "Generate Key", window.Fullname);

            generatekey.AskPassphrase = GUIUtils.AskPassphrase(owner);
            generatekey.ExecuteAsync(result =>
            {
                switch (result.Status)
                {
                    case GpgInterfaceStatus.Error:
                    {
                        if (result.Message == GpgInterfaceMessage.KeyNotCreated)
                            CustomMessageBox.Show(L10N.T("key_not_created"), L10N.T("key_not_created_title"), MessageBoxButton.OK, MessageBoxImage.Error);
                        break;
                    }

                    case GpgInterfaceStatus.Success:
                    {
                        CustomMessageBox.Show(L10N.T("key_created"), L10N.T("key_created_title"), MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
                        KeyManager.RefreshAll();
                        break;
                    }
                }
            });
        }
    }
}
